package com.mini.framework.db.dbutil;

import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


public  class DBUtilDaoSuppor {
	
	private static final String confPath = "/conf.properties";
	
	private static final String DB = "db";
	
	private static DBUtilDaoSuppor instance;
	
	private DataSource dataSource;
	
	private PageQueryer pager;
	
	public DBUtilDaoSuppor(){
		Properties properties = new Properties();
		try{
			properties.load(DBUtilDaoSuppor.class.getResourceAsStream(confPath));
			this.dataSource =  BasicDataSourceFactory.createDataSource(properties);
			DBEnum db = DBEnum.getDB(properties.getProperty(DB));
			if(db == DBEnum.MYSQL){
				pager = new MySqlPageQueryer();
			}else if(db == DBEnum.ORACLE){
				pager = new OraclePageQueryer();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	
	public static DBUtilDaoSuppor getInstance(){
		if(instance == null){
			instance = new DBUtilDaoSuppor();
		}
		return instance;
	}
	
	/**
	 * select count.
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int count(String sql,Object[] params){
		int count = 0;
		try{
			QueryRunner queryRunner = new QueryRunner(this.dataSource);
			Object result = queryRunner.query(sql, new ScalarHandler(1),params);
			if(result instanceof Integer){
				count = (Integer)result;
			}
			if(result instanceof Long){
				Long l = (Long)result; 
				count= l.intValue();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * 添加.
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int save(String sql, Object[] params) {
		int row = update(sql, params);
		return row;
	}

	/**
	 * 更新.
	 * 
	 * @param sql
	 * @param params
	 * @return 受影响的行数
	 */
	public int update(String sql, Object[] params) {
		QueryRunner queryRunner = new QueryRunner(this.dataSource);
		int affectedRows = 0;
		try {
			if (params == null) {
				affectedRows = queryRunner.update(sql);
			} else {
				affectedRows = queryRunner.update(sql, params);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return affectedRows;
	}

	/**
	 * 查询操作
	 * 
	 * @param sql
	 * @param param
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List query(String sql, Class<?> clazz, Object... params) {
		try {
			QueryRunner qRunner = new QueryRunner(this.dataSource);
			List list = (List) qRunner.query(sql, new BeanListHandler(clazz),params);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 分页查询.
	 * 
	 * @param sql
	 * @param clazz
	 * @param pageNo
	 * @param pageSize
	 * @param params
	 * @return
	 */
	public Page pageQuery(String sql,Class<?> clazz,int pageNo,int pageSize,Object...params){
		Page page = new Page();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		QueryRunner qRunner = new QueryRunner(this.dataSource);
		pager.pageQuery(sql, qRunner, clazz, pageNo, pageSize, page, params);
		return page;
	}
	
	
	
	

}
